package com.cx.controller;

import com.cx.pojo.Goods;
import com.cx.pojo.GroupGoods;
import com.cx.pojo.SeckillGoods;
import com.cx.pojo.Users;
import com.cx.service.IndexService;
import com.cx.vo.GroupGoodsVO;
import com.cx.vo.SeckillGoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/home")
public class IndexController {
    @Autowired
    private IndexService indexService;


    /**
     * 首页展示
     * @param seckillGoods
     * @param request
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/page")
    public String seckillGoods(SeckillGoods seckillGoods, HttpServletRequest request, Model model, HttpSession session) {
        List<SeckillGoods> seckillGoodsList= indexService.selectSeckillGoods();
        System.out.println("秒杀商品有："+seckillGoodsList);
        List<Goods> goodsList=new ArrayList<>();
        Goods goods=new Goods();

        List<SeckillGoodsVO> seckillGoodsVOList=new ArrayList<>();





        //秒杀商品
        for (int i = 0; i <seckillGoodsList.size() ; i++) {
            goods=indexService.findByGoodId(seckillGoodsList.get(i).getGoodId());
            SeckillGoodsVO seckillGoodsVO=new SeckillGoodsVO();
            seckillGoodsVO.setSeckillStock(seckillGoodsList.get(i).getSeckillStock());
            seckillGoodsVO.setGoodPrice(goods.getGoodPrice());
            seckillGoodsVO.setGoodImages(goods.getGoodImages());
            seckillGoodsVO.setGoodName(goods.getGoodName());
            seckillGoodsVO.setSeckillPrice(seckillGoodsList.get(i).getSeckillPrice());
            seckillGoodsVO.setGoodId(goods.getId());
            seckillGoodsVO.setSeckillNum(seckillGoodsList.get(i).getSeckillNum());
            seckillGoodsVOList.add(seckillGoodsVO);
        }
        model.addAttribute("seckillGoodsVOList",seckillGoodsVOList);

        //普通商品
        List<Goods> goodsList1=new ArrayList<>();
        goodsList1=indexService.selectGoods();
        model.addAttribute("goodsList1",goodsList1);

        //团购商品
        List<GroupGoodsVO> groupGoodsVOList =new ArrayList<>();
        groupGoodsVOList=indexService.selectGroupGoods();
        System.out.println(groupGoodsVOList.toString());
        model.addAttribute("groupGoodsVOList",groupGoodsVOList);




     return "index";
    }


    @RequestMapping("/search")
    @ResponseBody
    public List<Goods>  searchGoods(HttpServletRequest request, Model model, HttpSession session,Goods goods){

        String goodName =request.getParameter("goodName");
        System.out.println("搜索的商品是"+goodName);
        List<Goods> searchGoods =indexService.selectByGoodName(goodName);
        session.setAttribute("searchGoods",searchGoods);
        model.addAttribute("searchGoods",searchGoods);
        System.out.println("查出来的商品是"+searchGoods.toString());

        return searchGoods;







    }




    /**
     * 注销登录
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/home/page";


    }







}
